﻿using Anley.DomainDrivenDesign.Contract.Enums;

namespace Anley.DomainDrivenDesign.Contract.Application;

/// <summary>
/// 含数据的结果类
/// </summary>
/// <typeparam name="T">输出结果类型</typeparam>
public class AneResult<TData>
    : AneResult
{
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="code">状态码</param>
    /// <param name="message">消息</param>
    /// <param name="data">输出数据</param>
    public AneResult(
        int code,
        string message,
        TData data)
        : base(code, message)
    {
        Data = data;
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="content">输出数据</param>
    public AneResult(
        TData content)
        : this((int)AneResultCode.Success, string.Empty, content)
    {
    }

    /// <summary>
    /// 输出数据
    /// </summary>
    public TData Data { get; private set; }

    /// <summary>
    /// 成功
    /// </summary>
    /// <param name="data">输出数据</param>
    /// <param name="message">消息</param>
    public static AneResult<TData> Ok(
        TData data,
        string? message = null)
    {
        return new AneResult<TData>(data)
        {
            Message = string.IsNullOrWhiteSpace(message) ? string.Empty : message
        };
    }

    public static implicit operator bool(AneResult<TData> result)
    {
        return result.Code == (int)AneResultCode.Success;
    }
}